/**
 * 员工管理
 * 
 * @return {}
 */
var priAdminsSearch = function() {
	// 角色加载
	var roleNameStroe = new Ext.data.Store({
				proxy : new Ext.data.HttpProxy({
							url : __ctxPath + "/systemRole/selectAllRoles"
						}),
				reader : new Ext.data.JsonReader({
							root : 'list',
							fields : [{
										name : "id"
									}, {
										name : "name"
									}]
						})
			});
	// 部门
	var adminDeptUrl = __ctxPath + "/systemDept/selectDeptTreeById?deptRoot="
			+ curUserInfo.deptId;
	var adminDept = new TreeSelector("treeAdminDept", adminDeptUrl, "所属部门",
			"priAdmins_deptId", true, 180);
	var priAdminsField = new Ext.form.FieldSet({
				title : '查询条件',
				id : 'priAdminsField',
				collapsible : true,
				buttonAlign : 'center',
				items : [{
							layout : 'column',
							border : false,
							items : [{
										columnWidth : .25,
										layout : 'form',
										border : false,
										items : [{
													xtype : 'numberfield',
													name : 'id',
													anchor : '100%',
													fieldLabel : '账号ID'
												}]
									}, {
										columnWidth : .25,
										layout : 'form',
										border : false,
										items : [{
													xtype : 'textfield',
													name : 'name',
													anchor : '100%',
													fieldLabel : '登录名'
												}]
									}, {
										columnWidth : .25,
										layout : 'form',
										border : false,
										items : [{
													xtype : 'textfield',
													name : 'trueName',
													anchor : '100%',
													fieldLabel : '姓名'
												}]
									}, {
										columnWidth : .25,
										layout : 'form',
										border : false,
										items : [adminDept]
									}]
						}, {
							layout : 'column',
							border : false,
							items : [{
								columnWidth : .25,
								layout : 'form',
								border : false,
								items : [{
									xtype : "combo",
									fieldLabel : "状态",
									anchor : '100%',
									hiddenName : "isUseable",
									emptyText : "选择状态",
									store : new Ext.data.ArrayStore({
												fields : ["value", "text"],
												data : [['1', "可用"],
														['0', "禁用"], ['', '全部']]
											}),
									valueField : "value",
									displayField : "text",
									mode : "local",
									value : '1',
									triggerAction : "all"
								}]
							}, {
								columnWidth : .25,
								layout : 'form',
								border : false,
								items : [{
											xtype : 'combo',
											anchor : '100%',
											fieldLabel : "角色名称",
											hiddenName : "roleName",
											displayField : "name",
											valueField : "name",
											triggerAction : "all",
											emptyText : "请选择",
											anchor : '100%',
											editable : false,
											mode : 'remote',
											store : roleNameStroe
										}]
							}, {
								columnWidth : .25,
								layout : 'form',
								border : false,
								items : [{
											name : "deptId",
											id : "priAdmins_deptId",
											anchor : '100%',
											xtype : "hidden"
										}]
							}, {
								columnWidth : .25,
								layout : 'form',
								border : false,
								items : [{
											name : "priAdminVO.postId",
											id : "priAdmins_postId",
											anchor : '100%',
											xtype : "hidden"
										}]
							}, {
								columnWidth : .25,
								layout : 'column',
								border : false,
								items : [{
											text : '查询',
											xtype : 'button',
											width : 80,
											style : {
												marginLeft : "60px",
												marginRight : "10px"
											},
											handler : function() {
												queryPriAdmins();
											}

										}, {
											text : '重置',
											xtype : 'button',
											width : 80,
											handler : function() {
												Ext.getCmp('priAdminsForm')
														.getForm().reset();
											}
										}]

							}]
						}]
			});
	fieldSetListeners(['priAdminsField', 'priAdminsForm', 'priAdminsGrid'], [
					0.20, 0.80]);
	var priAdminsForm = new Ext.form.FormPanel({
				id : 'priAdminsForm',
				anchor : '100% 20%',
				frame : true,
				labelAlign : 'right',
				bodyBorder : false,
				keys : [{
							key : 13, // 13代表回车
							fn : queryPriAdmins,
							scope : this
						}],
				items : [priAdminsField]
			})
	return priAdminsForm;
}

var priAdminsResult = function() {
	var priAdminsSm = new Ext.grid.CheckboxSelectionModel();// 创建选择模式对象
	var priAdminsCm = new Ext.grid.ColumnModel([// 定义表的每列（对应表头信息）
	priAdminsSm, new Ext.grid.RowNumberer(), {
				header : "人员账号",
				sortable : true,
				width : 70,
				dataIndex : "id"
			}, {
				header : "登录名",
				sortable : true,
				width : 100,
				dataIndex : "name"
			}, {
				header : "姓名",
				width : 100,
				dataIndex : "trueName"
			}, {
				header : "所属部门",
				sortable : true,
				width : 70,
				dataIndex : "deptName"
			}, {
				header : "职位",
				sortable : true,
				width : 100,
				dataIndex : "postName",
				renderer : function(value, meta, record) {
					if (value) {
						meta.attr = 'ext:qtitle="职位:" ext:qtip="' + value + '"'
								+ 'style="white-space:normal;"';
						return value;
					}
				}
			}, {
				header : "角色",
				sortable : true,
				width : 100,
				dataIndex : "roleName",
				renderer : function(value, meta, record) {
					if (value) {
						meta.attr = 'ext:qtitle="角色:" ext:qtip="' + value + '"'
								+ 'style="white-space:normal;"';
						return value;
					}
				}
			}, {
				header : "邮箱",
				width : 130,
				dataIndex : "email"
			}, {
				header : "创建时间",
				width : 130,
				sortable : true,
				dataIndex : "createdAt"
			}, {
				header : "是否订阅消息",
				width : 70,
				dataIndex : "isSubscription",
				renderer : function(a) {
					var b = "";
					if (a == 0) {
						b += '否';
					} else {
						b += '<font color=\"#FF0000\" >是</font>';
					}
					return b;
				}
			}, {
				header : "状态",
				width : 70,
				dataIndex : "isUseable",
				renderer : function(a) {
					var b = "";
					if (a == "1") {
						b += '可用';
					} else {
						b += '禁用';
					}
					return b;
				}
			}, {
				header : "管理",
				width : 250,
				dataIndex : "userId",
				sortable : false,
				renderer : function(j, i, g, m, h) {
					var l = g.data.id;
					var f = g.data.name;
					var isSubscription = g.data.isSubscription;
					var k = "";
					if (l != 0) {
						if (isGranted("adminsManage_修改人员")) {
							k += '&nbsp;&nbsp;<a href="#" onclick="updatePriAdmin('
									+ l + ');" >修改</a>';
						}
						if (isGranted("adminsManage_人员分配角色")) {
							k += '&nbsp;<a href="#" onclick="assign(' + l
									+ ",'" + f + "')\">分配角色</a>";
						}
						if (isGranted("adminsManage_查看功能")) {
							k += '&nbsp;<a href="#" onclick="lookFunctions('
									+ l + ",'" + f + "')\">查看功能</a>";
						}
						if (isGranted("adminsManage_订阅消息")) {
							if (isSubscription == 0) {
								k += '&nbsp;<a href="#" onclick="updateUserSubscription('
										+ l + ",'1','订阅')\">订阅消息</a>";
							} else if (isSubscription == 1) {
								k += '&nbsp;<a href="#" onclick="updateUserSubscription('
										+ l + ",'0','取消')\">取消订阅</a>";
							}
						}
						if (isGranted("adminsManage_设置订阅消息")) {
							if (isSubscription == 1) {
								k += '&nbsp;<a href="#" onclick="setUserSubscription('
										+ l + ",'" + f + "')\">消息设置</a>";
							}
						}
					}
					return k;
				}
			}]);

	// 定义分析器
	var priAdminsReader = new Ext.data.JsonReader({
				totalProperty : "totalCount",
				root : "list",
				fields : [{
							name : "id"
						}, {
							name : "name"
						}, {
							name : "trueName"
						}, {
							name : "deptName"
						}, {
							name : "email"
						}, {
							name : "createdAt"
						}, {
							name : "updatedAt"
						}, {
							name : "isUseable"
						}, {
							name : "postName"
						}, {
							name : 'postId'
						}, {
							name : 'deptId'
						}, {
							name : 'postName'
						}, {
							name : 'roleName'
						}, {
							name : 'isSubscription'
						}]
			});

	var adminsViewStore = new Ext.data.Store({// 取数据
		autoLoad : true,
		proxy : new Ext.data.HttpProxy({
					url : __ctxPath + "/systemAdmin/selectAllAdmin"
				}),
		remoteSort : false,
		baseParams : {
			start : 0,
			limit : 25
		},
		listeners : {
			beforeload : {
				fn : function(thiz, options) {
					Ext.apply(thiz.baseParams, Ext.getCmp('priAdminsForm')
									.getForm().getValues());
				}
			}
		},
		reader : priAdminsReader
	});
	var priAdminsTb = new Ext.Toolbar({// 定义grid的toolbar
		id : "priAdminsTb",
		height : 25,
		items : []
	});
	if (isGranted("adminsManage_新建人员")) {
		priAdminsTb.add(new Ext.Button({
					text : "新建人员",
					width : 80,
					iconCls : "btn-add",
					pressed : true,
					listeners : {
						"click" : function() {
							addPriAdmin();
						}
					}
				}));
	}
	var priAdminsGrid = new Ext.grid.GridPanel({
				id : 'priAdminsGrid',
				store : adminsViewStore,
				cm : priAdminsCm,
				sm : priAdminsSm,
				autoScroll : true,
				loadMask : true,
				stripeRows : true,
				columnLines : true,
				loadMask : {
					msg : "正在加载数据，请稍侯……"
				},
				anchor : '100% 80%',
				frame : true,
				tbar : priAdminsTb,
				bbar : new PG.PagingBar({
							store : adminsViewStore
						})
			});
	return priAdminsGrid;
};
// 查询
function queryPriAdmins() {
	var grid = Ext.getCmp("priAdminsGrid");
	var form = Ext.getCmp("priAdminsForm");
	if (form.getForm().isValid()) {
		grid.getStore().load();
	}
}
// 新建
function addPriAdmin() {
	Ext.Loader.load([__ctxPath + '/js/system/adminForm.js'], function() {
				addPriAdminWin().show();
			});
}
// 修改
function updatePriAdmin(adminId) {
	Ext.Loader.load([__ctxPath + '/js/system/adminForm.js'], function() {
				addPriAdminWin(adminId).show();
			});
}
// 分配角色
function assign(pid, name) {
	Ext.Ajax.request({
				url : __ctxPath + "/systemRole/selectRoleByAdminId",
				method : "post",
				params : {
					adminId : pid
				},
				success : function(c, e) {
					var g = Ext.util.JSON.decode(c.responseText);
					Ext.Loader.load([__ctxPath
									+ '/js/system/adminAssignRoleView.js'],
							function() {
								AdminAssignRoleView.getView(pid, name, g.list)
										.show();
							});
				}
			});

}
// 查看人员功能
function lookFunctions(id, name) {
	Ext.Loader.load([__ctxPath + '/js/system/lookAdminFunctions.js'],
			function() {
				lookAdminFunctions(id, name).show();
			});

};
/**
 * 消息提醒设置
 * 
 * @param {}
 *            id
 */
function setUserSubscription(userId,userName) {
	// 查询我已选择的消息类型
	Ext.Ajax.request({
				url : __ctxPath + "/systemAdmin/SelectUserSubscript",
				method : "POST",
				params : {
					userId : userId
				},
				success : function(c, e) {
					var g = Ext.util.JSON.decode(c.responseText);
					Ext.Loader.load([__ctxPath
									+ '/js/system/setUserSubWindow.js'],
							function() {
								setUserSubscriberWindow(userId,userName, g.list).show();
							});
				}
			});

}
/**
 * 设置取消提醒
 * 
 * @param {}
 *            userId
 * @param {}
 *            isSubscription
 */
function updateUserSubscription(userId, isSubscription, message) {
	Ext.Msg.confirm("信息确认", "是否" + message + "提醒?", function(c) {
		if (c == "yes") {
			var optionMask = new Ext.LoadMask(Ext.getBody(), {
						msg : "正在处理,请稍后..."
					});
			optionMask.show();
			Ext.Ajax.request({
						url : __ctxPath + "/systemAdmin/updateUserSubscription",
						params : {
							userId : userId,
							isSubscription : isSubscription
						},
						method : "post",
						success : function(response) {
							optionMask.hide();
							var jsonData = Ext.util.JSON
									.decode(response.responseText);
							if (jsonData.success) {
								Ext.MessageBox.show({
											title : "信息提示",
											msg : jsonData.data,
											buttons : Ext.MessageBox.OK,
											icon : Ext.MessageBox.INFO
										});
								queryPriAdmins();
							} else {
								Ext.MessageBox.show({
											title : "错误提示",
											msg : jsonData.data,
											buttons : Ext.MessageBox.OK,
											icon : Ext.MessageBox.ERROR
										});
							}
						},
						failure : function(response) {
							optionMask.hide();
							Ext.MessageBox.show({
										title : "操作信息",
										msg : "服务器请求异常,请稍后再试!",
										buttons : Ext.MessageBox.OK,
										icon : Ext.MessageBox.ERROR
									});
						}
					});
		}
	});
};
Ext.getCmp("centerTabPanel").getActiveTab().add(priAdminsSearch(),
		priAdminsResult());
Ext.getCmp("centerTabPanel").getActiveTab().doLayout();